{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 类和实例\n",
    "\n",
    "面向对象最重要的概念就是类（Class）和实例（Instance），必须牢记**类是抽象的模板**，比如Student类，而实例是根据类创建出来的一个个具体的“对象”，每个对象都拥有相同的方法，但各自的数据可能不同。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 建立类和实例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Bart Simpson\n",
      "50\n",
      "13\n",
      "<__main__.Student object at 0x0000024CBA48D7B8>\n",
      "<class '__main__.Student'>\n"
     ]
    }
   ],
   "source": [
    "class Student(object): # 所有的类都继承object\n",
    "    def __init__(self, name, score): # 类初始化方法\n",
    "        self.name = name\n",
    "        self.score = score\n",
    "        \n",
    "bart = Student(\"Bart Simpson\", 50)\n",
    "bart.age = 13 # 给实例动态绑定属性\n",
    "print(bart.name)\n",
    "print(bart.score)\n",
    "print(bart.age)\n",
    "print(bart) # 打印bart的地址\n",
    "print(Student) # 打印Student类"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据封装\n",
    "\n",
    "面向对象编程的一个重要特点就是**数据封**；我们可以通过函数来访问这些数据，比如打印一个学生的成绩："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Lisa A\n",
      "Bart C\n"
     ]
    }
   ],
   "source": [
    "class Student(object):\n",
    "    def __init__(self, name, score):\n",
    "        self.name = name\n",
    "        self.score = score\n",
    "        \n",
    "    def print_score(self):\n",
    "        print(\"%s: %s\" % (self.name, self.score))\n",
    "        \n",
    "    def get_grade(self):\n",
    "        if self.score >= 90:\n",
    "            return 'A'\n",
    "        elif self.score >= 60:\n",
    "            return 'B'\n",
    "        else:\n",
    "            return 'C'\n",
    "        \n",
    "lisa = Student('Lisa', 99)\n",
    "bart = Student('Bart', 56)\n",
    "print(lisa.name, lisa.get_grade())\n",
    "print(bart.name, bart.get_grade())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "566px",
    "left": "174px",
    "top": "140.57px",
    "width": "150px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
